/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 jfinal app. jfapp Group.
 */

package app.models.activity;

import com.github.sog.annotation.TableBind;
import com.github.sog.config.StringPool;
import com.github.sog.controller.datatables.core.ColumnDef;
import com.github.sog.controller.datatables.core.DatatablesCriterias;
import com.github.sog.plugin.sqlinxml.SqlKit;
import com.google.common.base.Strings;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * <p>
 * 商场活动
 * The table ol_mall_activity mapping model.
 * </p>
 */
@TableBind(tableName = "ol_mall_activity")
public class MallActivity extends Model<MallActivity> {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(MallActivity.class);

    private static final long serialVersionUID = 1L;
    /**
     * The public dao.
     */
    public static final MallActivity dao = new MallActivity();


    public List<MallActivity> list() {
        return find(SqlKit.sql("mallactivity.list"));
    }


    public Page<MallActivity> datatable(DatatablesCriterias criterias, String name) {
        StringBuilder sb = new StringBuilder(" WHERE 1=1 ");
        if(!Strings.isNullOrEmpty(name)){
            if(!Strings.isNullOrEmpty(name)){
                sb.append(" AND ol_mall_activity.name like '%").append(name).append("%'");
            }
        }
        int pageSize = criterias.getDisplaySize();
        int start = criterias.getDisplayStart() / pageSize + 1;
        final List<ColumnDef> sortingColumnDefs = criterias.getSortingColumnDefs();

        String where = SqlKit.sql("mallactivity.columns-where") + sb.toString();
        logger.debug(where);

        if (sortingColumnDefs != null && !sortingColumnDefs.isEmpty()) {
            StringBuilder orderBy = new StringBuilder();
            for (ColumnDef sortingColumnDef : sortingColumnDefs)
                if (sortingColumnDef.isSortable()) {
                    orderBy.append(sortingColumnDef.getName()).append(StringPool.SPACE).append(sortingColumnDef.getSortDirection().toString());
                }
            final String byColumns = orderBy.toString();
            if (!Strings.isNullOrEmpty(byColumns)) {
                where += " ORDER BY " + byColumns;
            }
        }
        if (!where.contains("ORDER")) {
            where += " ORDER BY seq ASC,create_time DESC";
        }

        return paginate(start, pageSize, SqlKit.sql("mallactivity.columns-select"), where);
    }

    public boolean batch_stop(List<String> ids) {
        Object[][] objects = new Object[ids.size()][];
        Object[] obj;
        for (int i = 0; i < ids.size(); i++) {
            obj = new Object[1];
            obj[0] = ids.get(i);
            objects[i] = obj;
        }
        try {
            Db.batch(SqlKit.sql("mallactivity.stop"), objects, 50);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}